/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       gateway.cpp

	$Header: /game/gateway.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )
#include "gateway.h"

#include "adv_object_type.h"
#include "adventure_frame.h"
#include "adventure_ai.h"
#include "adventure_map.h"
#include "adventure_sounds.h"
#include "adv_object_type.h"
#include "adv_actor_model.h"
#include "army.h"
#include "bitmap_group_cache.h"
#include "dialog_teleporter_entrance.h"
#include "gateway_selection.h"
#include "gateway_vector.h"
#include "music.h"
#include "object_registration.h"
#include "player.h"
#include "simple_dialog.h"
#include "stationary_adventure_object.h"

// --------------------------------------------------------------------------
// Unnamed namespace
// --------------------------------------------------------------------------

namespace
{

	t_object_registration< t_gateway > g_gateway_registration( k_adv_object_gateway );

} // Unnamed namespace

t_gateway::t_gateway (  std::string const& model_name )
          : t_gateway_base( model_name )
{
}

void t_gateway::activate_trigger( t_army* army, t_adv_map_point const& point, 
		                           t_direction direction, t_adventure_frame* frame )
{
    t_gateway_vector gateway_list = m_map->get_gateways( get_gateway_type() );
	if (army->get_owner()->is_computer()) 
	{
		t_adventure_path const& path = m_map->get_ai().get_current_path();
		t_adv_map_point dest_point = point + get_direction_offset( direction );

		t_gateway_base const* base = ai_select_gateway_exit( path, gateway_list, this, army, dest_point );
		if (base)
			teleport_army ( *base, army, frame );
		return;
	}	

    int                         num_possible_gateways = 0;
    t_gateway_vector            possible_gateways;
    t_level_map_point_2d_list   gateway_points;
    std::vector<std::string>    gateway_names;
    int                         selected_gateway = 0;
    int                         i;

    for (i=0; i < gateway_list.size(); i++)
    {
	    if ( this != gateway_list[i].get())
        {
    	    gateway_points.push_back ( gateway_list[i]->get_position() );
            gateway_names.push_back ( gateway_list[i]->get_identifier() );

            // Create a list of possible gateways.
            possible_gateways.push_back ( gateway_list[i]) ;
        }
    }

	t_sound_cache music = get_music_playing();
	stop_music();
	get_dialog_sound( k_dialog_sound_portal )->play( get_sound_volume() );

	if ( possible_gateways.empty() )
	{
		ok_dialog( get_text("denied"), true, get_text("name") );
		play_music( music );
		return;
	}

    // Show gateway selection dialog window (if more than 1 - else, dialog just returns).
	t_counted_ptr<t_dialog_teleporter_entrance> dialog_ptr;

	dialog_ptr = new t_dialog_teleporter_entrance( frame );
	dialog_ptr->init_dialog( frame, gateway_points, gateway_names, 0, get_text ("Name"), get_text ("Initial") );
	dialog_ptr->run_modal();
	play_music( music );

    selected_gateway = (dialog_ptr->get_selected_exit());

    // Refused
    if (selected_gateway == -1)
        return;
    
    /*
    ** Teleport army.
    */
    teleport_army ( *possible_gateways[ selected_gateway ], army, frame );
}

void t_gateway::destroy()
{
	m_map->remove_gateway( this );

	t_gateway_base::destroy();
}

void t_gateway::place( t_adventure_map& map, t_adv_map_point const& point )
{
    t_gateway_base::place( map, point );
    
    // Add gateway.
    map.add_gateway ( this );
}

void t_gateway::pathing_destination_query( t_adventure_path_point const& source, 
										   t_adventure_path_finder & path_finder) const
{
	t_gateway_vector gateway_list = m_map->get_gateways( get_gateway_type() );

    for (int i=0; i < gateway_list.size(); i++)
    {
	    if ( this != gateway_list[i].get() )
        {
		    t_adv_map_point gateway_exit;
		    if ( gateway_list[i]->find_adjacent_space( gateway_exit, *path_finder.get_army() ) )
			{
				gateway_exit.on_bridge = false;

				path_finder.push( gateway_exit, source, 0 );
			}
        }
    } 
}

